package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.PageParams;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public int selectByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, categoryId);
        Integer rows = setmealMapper.selectCount(lqw);
        return rows;
    }

    /**
     * 保存套餐及其相关的菜品
     *
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐信息
        int rows = setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        /*if (rows > 0 && setmealDishes.size() > 0) {

        }*/
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public Page findPage(PageParams pageParams) {
        //查询套餐信息
        Page<Setmeal> setmealPage = new Page<>(pageParams.getPage(), pageParams.getPageSize());

        //构造条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //查询条件
        lqw.like(pageParams.getName() != null, Setmeal::getName, pageParams.getName());
        //查询套餐信息
        setmealMapper.selectPage(setmealPage, lqw);
        //创建SetmealDto 分页查询对象
        Page<SetmealDto> setmealDtoPage = new Page<>();
        setmealDtoPage.setTotal(setmealPage.getTotal());
        //要展示的套餐信息中有分类名称
        //根据分类id查询分类名称
        List<SetmealDto> list = setmealPage.getRecords().stream().map((setmeal) -> {
            SetmealDto setmealDto = new SetmealDto();
            //把setmeal里的数据拷贝到setmealDish中
            BeanUtils.copyProperties(setmeal, setmealDto);
            //查询分类名称
            Long categoryId = setmeal.getCategoryId();
            setmealDto.setCategoryName(categoryMapper.selectById(categoryId).getName());
            //设置套餐关联的菜品数
            List<SetmealDish> setmealDishes = setmealDishService.fiandBySetmealId(setmeal.getId());
            setmealDto.setSetmealDishes(setmealDishes);
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return setmealDtoPage;
    }

    @Override
    public int deleteSetmeal(List<Long> ids) {
        //构造条件器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);
        lqw.eq(Setmeal::getStatus, 1);
        Integer counts = setmealMapper.selectCount(lqw);
        if (counts > 0) {
            throw new CustomException("这个套餐启用状态，不能删除");
        }
        //如果可以删除，先删除套餐表中的数据
        int rows = setmealMapper.deleteBatchIds(ids);

        //在删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> dqw = new LambdaQueryWrapper<>();
        dqw.in(SetmealDish::getSetmealId, ids);
        int rowss = setmealDishMapper.delete(dqw);
        if (rows > 0 && rowss > 0) {
            return 1;
        } else {
            return 0;
        }

    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        slqw.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        slqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealMapper.selectList(slqw);
        return setmealList;
    }

    /**
     * 根据SetmealDto修改Setmeal和SetmealDish
     *
     * @param setmealDto
     * @return
     */
    @Override
    public int updatSetmeal(SetmealDto setmealDto) {
        //修改套餐，就要把套餐表的数据和套餐关联的菜品保存到数据库
        //1.1修改套餐
        int rows = setmealMapper.updateById(setmealDto);
        //2.1修改套餐相关联的菜品
        //2.2清除当前数据库里的菜品数
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishMapper.delete(lqw);
        //2.2获取所有套餐的商品数
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            //设置菜品属于哪个套餐
            item.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(item);
            return item;
        }).collect(Collectors.toList());
       /* //2.2把套餐相关联的菜品保存到数据库
        int row2 = 0;
        for (SetmealDish setmealDish : setmealDishes) {
            row2 += setmealDishMapper.insert(setmealDish);
        }
        if (rows>0&&row2>0){
            return 1;
        }else {
            return 0;
        }*/
        if (rows > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改套餐状态
     *
     * @param ids    修改id
     * @param status 要修改成的状态值
     * @return
     */

    @Override
    public int updatSetmealStatus(List<Long> ids, Integer status) {
        int row = 0;
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            setmeal.setStatus(status);
            row += setmealMapper.updateById(setmeal);
        }
        return row;
    }

    @Override
    public SetmealDto fiandSetmealDto(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setImage(setmeal.getImage());
        setmealDto.setPrice(setmeal.getPrice());
        List<SetmealDish> setmealDishes = setmealDishService.fiandBySetmealId(id);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    /**
     * 根据套餐id获取套餐
     * @param id
     * @return
     */

    @Override
    public Setmeal findSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        return setmeal;
    }


}
